from django.db import models
from django.utils import timezone

# 基础模型
class BaseModel(models.Model):
    id = models.AutoField(primary_key=True)
    create_time = models.DateTimeField(default=timezone.now, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        abstract = True

# 机器人密钥信息表
BOT_API_KEY_LEVEL = [
    ('0', '管理员'),
    ('1', '普通用户'),
    ('2', '会员用户'),
]
BOT_API_KEY_STATE = [
    ('init', '待激活'),
    ('active', '已激活'),
    ('inactive', '已失效'),
    ('delete', '已删除'),
]
class BotApiKey(BaseModel):
    # 应用的唯一标识
    api_key = models.CharField(max_length=200)
    # API_KEY 对应的等级
    level = models.CharField(max_length=20, default="0", choices=BOT_API_KEY_LEVEL)
    # API_KEY 的状态
    state = models.CharField(max_length=20, default="init", choices=BOT_API_KEY_STATE)
    # 绑定的用户 id
    user_id = models.CharField(max_length=200, default="0")

    class Meta:
        db_table = 'tb_bot_api_key'
        verbose_name = '机器人使用密钥表'
        verbose_name_plural = verbose_name

# 机器人信息表
BOT_TYPE = [
    ('wx_gpt', '微信 gpt 机器人'),
    ('wx_mj', '微信 mj 机器人'),
    ('qywx_gpt', '企业微信 gpt 机器人'),
    ('qywx_mj', '企业微信 mj 机器人'),
]
class BotInfo(BaseModel):
    # 应用的唯一标识
    api_key = models.ForeignKey(BotApiKey, on_delete=models.CASCADE)
    # 机器人类型
    bot_type = models.CharField(max_length=1000, default="wx_gpt", choices=BOT_TYPE)
    # 机器人远程主机名
    hostname = models.CharField(max_length=200, default="0")
    # 机器人远程主机用户名
    username = models.CharField(max_length=200, default="0")
    # 机器人远程主机密码
    password = models.CharField(max_length=200, default="0")
    # 机器人远程主机路径
    bot_path = models.CharField(max_length=200, default="0")
    # 机器人远程主机进程号
    process_id = models.CharField(max_length=10, default="0")
    # 机器人额外信息，存储格式是 json 串，json.dumps()
    extra = models.TextField(max_length=65535, default="")

    class Meta:
        db_table = 'tb_bot_info'
        verbose_name = '机器人信息表'
        verbose_name_plural = verbose_name

# 机器人消息表
BOT_MESSAGE_STATE = [
    ('waiting', '待处理'),
    ('ignore', '已忽略'),
    ('done', '已处理'),
]
BOT_MESSAGE_SOURCE = [
    ('group', '群聊'),
    ('single', '私聊'),
]
class BotMessage(BaseModel):
    # 机器人信息
    bot_info = models.ForeignKey(BotInfo, on_delete=models.CASCADE)
    # 消息来源
    source = models.CharField(max_length=20, default="group", choices=BOT_MESSAGE_SOURCE)
    # 机器人消息的状态
    state = models.CharField(max_length=20, default="waiting", choices=BOT_MESSAGE_STATE)
    # 机器人消息的原始内容
    message = models.CharField(max_length=8000, default="{}")

    class Meta:
        db_table = 'tb_bot_message'
        verbose_name = '机器人消息表'
        verbose_name_plural = verbose_name

# 机器人行为表
BOT_ACTION_STATUS = [
    ('cancel', '已取消'),
    ('waiting', '等待执行'),
    ('done', '已完成'),
]
BOT_ACTION_NAME = [
    ('send_text', '发送文本'),
    ('send_image', '发送图片'),
]
class BotActions(BaseModel):
    # 机器人信息
    bot_info = models.ForeignKey(BotInfo, on_delete=models.CASCADE)
    # 操作名
    action_name = models.CharField(max_length=100, default="send_text", choices=BOT_ACTION_NAME)
    # 操作内容，send_text 对应文字消息，send_image 对应图片链接
    action_content = models.CharField(max_length=5000, default="暂无操作内容")
    # 用户 id
    to_user = models.CharField(max_length=500, default="send_text")
    # 机器人行为的状态
    status = models.CharField(max_length=20, default="waiting", choices=BOT_ACTION_STATUS)

    class Meta:
        db_table = 'tb_bot_actions'
        verbose_name = '机器人行为表'
        verbose_name_plural = verbose_name

# mj 任务表
MJ_TASK_STATUS = [
    ('sent', '已发送'),
    ('start', '已开始'),
    ('stop', '已中止'),
    ('done', '已完成'),
]
class MJTasks(BaseModel):
    # 机器人信息
    bot_info = models.ForeignKey(BotInfo, on_delete=models.CASCADE)
    # 原始的 prompt，限制长度 6000
    origin_prompt = models.TextField(max_length=6000, default="")
    # 翻译的 prompt，限制长度 8000
    trans_prompt = models.TextField(max_length=8000, default="")
    # 来源的 id ，私聊的是 user_id，群聊的是 group_id
    from_id = models.CharField(max_length=1000, default="")
    # 来源的昵称
    from_nickname = models.CharField(max_length=1000, default="")
    # 原始图片路径
    origin_image_url = models.CharField(max_length=1000, default="")
    # 对象存储图片路径
    oss_image_url = models.CharField(max_length=1000, default="")
    # 记录已完成的消息
    mj_done_msg = models.TextField(max_length=65535, default="")
    # 任务状态
    status = models.CharField(max_length=20, default="sent", choices=MJ_TASK_STATUS)

    class Meta:
        db_table = 'tb_mj_tasks'
        verbose_name = 'mj 任务信息表'
        verbose_name_plural = verbose_name

# 敏感词信息表
class SensitiveWord(BaseModel):
    word = models.CharField(max_length=20)

    class Meta:
        db_table = 'tb_sensitive_word'
        verbose_name = '敏感词配置表'
        verbose_name_plural = verbose_name

# gpt 任务表

